#if defined(debugging)

#include <iostream>
#include <stdlib.h>

// Do we need our own allocators?
// Sometimes, when we act as an apache module and need to grab memory from a SHM pool.

int nAllocations = 0;

char *bigChunk = 0, *chunkEnd; // char as unit to cast less in calculations

char *slAlloc (size_t s) {
  char *ret = (char*) malloc (s);

  /*  s = (s + 15) & -15;

   // The 'Hey Big Spender!' allocator
   const int chunkSize = 33554432; // That's 32MB. Will get used up fairly quick!
   if(!bigChunk) {
   bigChunk = (char*) malloc (chunkSize * sizeof(char));
   chunkEnd = bigChunk + chunkSize;
   }

   char *ret = bigChunk;
   bigChunk += s; // Reduces the chunk
   if(bigChunk > chunkEnd) ret = 0;*/

#if defined(debugging)
  nAllocations++; // Without locking, this may go weird if multithreading
  return ret;
#endif
}

void slFree (char *p) {
  free (p);

#if defined(debugging)
  if(--nAllocations == 0)
  std::cerr << "<Heap cleaned>" << std::endl;
#endif
}

void *operator new (size_t s) {return slAlloc (s);}
void *operator new [] (size_t s) {return slAlloc (s);}
void operator delete (void* p) {slFree ((char*) p);}

#endif
